package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.visa.info.SupplierSystemOrderVisaDetailInfoDTO;
import com.ctshk.rpc.booking.dto.system.visa.info.SupplierSystemOrderVisaInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderVisa;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderVisaDetail;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderVisaDetailMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderVisaMapper;
import com.ctshk.rpc.booking.req.system.visa.add.SupplierSystemOrderVisaAddReq;
import com.ctshk.rpc.booking.req.system.visa.add.SupplierSystemOrderVisaDetailAddReq;
import com.ctshk.rpc.booking.req.system.visa.edit.SupplierSystemOrderVisaEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderVisaService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统内签证基础信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-25
 */
@DubboService
public class SupplierSystemOrderVisaServiceImpl implements ISupplierSystemOrderVisaService {
    @Autowired
    SupplierSystemOrderVisaMapper supplierSystemOrderVisaMapper;
    @Autowired
    SupplierSystemOrderVisaDetailMapper supplierSystemOrderVisaDetailMapper;

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderVisaAddReq> supplierSystemOrderVisaAddReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderVisaAddReqList)) {
            List<SupplierSystemOrderVisa> supplierSystemOrderVisaList = new ArrayList<>();
            supplierSystemOrderVisaAddReqList.forEach(visaAddReq -> {
                SupplierSystemOrderVisa supplierSystemOrderVisa = EntityUtil.copy(visaAddReq, SupplierSystemOrderVisa.class);
                supplierSystemOrderVisa.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderVisa.setOrderId(id);
                supplierSystemOrderVisa.setCreateId(userId);
                supplierSystemOrderVisa.setGmtCreate(nowDateTime);
                supplierSystemOrderVisa.setModifiedId(userId);
                supplierSystemOrderVisa.setGmtModified(nowDateTime);
                supplierSystemOrderVisa.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderVisaList.add(supplierSystemOrderVisa);
                //添加旅客
                List<SupplierSystemOrderVisaDetailAddReq> supplierSystemOrderVisaDetailAddReqList = visaAddReq.getSupplierSystemOrderVisaDetailAddReqList();
                if (CollectionUtils.isNotEmpty(supplierSystemOrderVisaDetailAddReqList)) {
                    List<SupplierSystemOrderVisaDetail> supplierSystemOrderVisaDetails = new ArrayList<>();
                    supplierSystemOrderVisaDetailAddReqList.forEach(visaDetailAdd -> {
                        SupplierSystemOrderVisaDetail supplierSystemOrderVisaDetail = EntityUtil.copy(visaDetailAdd, SupplierSystemOrderVisaDetail.class);
                        supplierSystemOrderVisaDetail.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderVisaDetail.setOrderId(id);
                        supplierSystemOrderVisaDetail.setVisaId(supplierSystemOrderVisa.getId());
                        supplierSystemOrderVisaDetail.setCreateId(userId);
                        supplierSystemOrderVisaDetail.setGmtCreate(nowDateTime);
                        supplierSystemOrderVisaDetail.setModifiedId(userId);
                        supplierSystemOrderVisaDetail.setGmtModified(nowDateTime);
                        supplierSystemOrderVisaDetail.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderVisaDetails.add(supplierSystemOrderVisaDetail);
                    });
                    supplierSystemOrderVisaDetailMapper.insertBatchSomeColumn(supplierSystemOrderVisaDetails);
                }
            });
            supplierSystemOrderVisaMapper.insertBatchSomeColumn(supplierSystemOrderVisaList);
        }
    }

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderVisaEditReq> supplierSystemOrderVisaEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderVisaEditReqList)) {
            supplierSystemOrderVisaEditReqList.forEach(visaEditReq -> {
                UpdateWrapper<SupplierSystemOrderVisa> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderVisa::getId, visaEditReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderVisa::getCustomerRemark, visaEditReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderVisa::getSupplierRemark, visaEditReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderVisa::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderVisa::getGmtModified, nowDateTime);
                supplierSystemOrderVisaMapper.update(new SupplierSystemOrderVisa(), updateWrapper);
                visaEditReq.getSupplierSystemOrderVisaDetailEditReqList().forEach(visaDetailReq -> {
                    UpdateWrapper<SupplierSystemOrderVisaDetail> updateDetailWrapper = new UpdateWrapper<>();
                    updateDetailWrapper.lambda().eq(SupplierSystemOrderVisaDetail::getId, visaDetailReq.getId());
                    updateDetailWrapper.lambda().set(SupplierSystemOrderVisaDetail::getPrice, visaDetailReq.getPrice());
                    updateDetailWrapper.lambda().set(SupplierSystemOrderVisaDetail::getModifiedId, userId);
                    updateDetailWrapper.lambda().set(SupplierSystemOrderVisaDetail::getGmtModified, nowDateTime);
                    supplierSystemOrderVisaDetailMapper.update(null, updateDetailWrapper);
                });
            });
        }
    }

    @Override
    public List<SupplierSystemOrderVisaInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderVisa> visaQueryWrapper = new QueryWrapper<>();
        visaQueryWrapper.lambda().eq(SupplierSystemOrderVisa::getOrderId, id);
        List<SupplierSystemOrderVisa> supplierSystemOrderVisaList = supplierSystemOrderVisaMapper.selectList(visaQueryWrapper);
        List<SupplierSystemOrderVisaInfoDTO> supplierSystemOrderVisaInfoDTOList = EntityUtil.copyList(supplierSystemOrderVisaList, SupplierSystemOrderVisaInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderVisaInfoDTOList)) {
            supplierSystemOrderVisaInfoDTOList.forEach(visaCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderVisaDetail> visaDetailQueryWrapper = new QueryWrapper<>();
                visaDetailQueryWrapper.lambda().eq(SupplierSystemOrderVisaDetail::getOrderId, id);
                visaDetailQueryWrapper.lambda().eq(SupplierSystemOrderVisaDetail::getVisaId, visaCheckinDTO.getId());
                List<SupplierSystemOrderVisaDetail> supplierSystemOrderVisaDetails = supplierSystemOrderVisaDetailMapper.selectList(visaDetailQueryWrapper);
                List<SupplierSystemOrderVisaDetailInfoDTO> supplierSystemOrderVisaDetailInfoDTOS = EntityUtil.copyList(supplierSystemOrderVisaDetails, SupplierSystemOrderVisaDetailInfoDTO.class);
                visaCheckinDTO.setSupplierSystemOrderVisaDetailInfoDTOList(supplierSystemOrderVisaDetailInfoDTOS);
            });
        }
        return supplierSystemOrderVisaInfoDTOList;
    }
}
